package bufferings.lab.basic.code;

import static com.google.appengine.api.datastore.Query.FilterOperator.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.slim3.tester.AppEngineTestCase;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;

public class PrefixScanCode extends AppEngineTestCase {

  public static final String KIND = PrefixScanCode.class.getSimpleName();

  public static final String PROP_NAME = "prop1";

  @Test
  public void プロパティ値の前方一致検索() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

    Entity e = new Entity(KIND, "prefix-123");
    e.setProperty(PROP_NAME, "prefix-123");
    ds.put((Transaction) null, e);

    Query q = new Query(KIND);
    q.addFilter(PROP_NAME, GREATER_THAN_OR_EQUAL, "prefix-");
    q.addFilter(PROP_NAME, LESS_THAN, "prefix-\ufffd");
    assertNotNull(ds.prepare(q).asSingleEntity());
  }

  @Test
  public void キー名の前方一致検索() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity e = new Entity(KIND, "prefix-123");
    e.setProperty(PROP_NAME, "prefix-123");
    ds.put((Transaction) null, e);

    Query q = new Query(KIND);
    q.addFilter(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN_OR_EQUAL, KeyFactory
      .createKey(KIND, "prefix-"));
    q.addFilter(Entity.KEY_RESERVED_PROPERTY, LESS_THAN, KeyFactory.createKey(
      KIND,
      "prefix-\ufffd"));
    assertNotNull(ds.prepare(q).asSingleEntity());
  }

}
